package
{
	import away3d.cameras.Camera3D;
	import away3d.cameras.HoverCamera3D;
	import away3d.cameras.lenses.OrthogonalLens;
	import away3d.containers.View3D;
	import away3d.core.base.Mesh;
	import away3d.core.math.Matrix3DUtils;
	import away3d.core.render.Renderer;
	import away3d.core.utils.Cast;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.Material;
	import away3d.materials.WireframeMaterial;
	import away3d.primitives.Plane;
	import away3d.primitives.Sphere;
	import away3d.sprites.Sprite3D;
	import away3d.tools.utils.Drag3D;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	import flash.net.URLRequest;
	
	import jiglib.physics.MaterialProperties;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.away3d.Away3DPhysics;
	import jiglib.plugin.away3d.Away3dMesh;
	
	public class bmoregolf extends Sprite
	{
		private var camera:Camera3D;
		private var view:View3D; 
		private var physics:Away3DPhysics;
		private var m_inputManager:InputManager;
		private var m_box:RigidBody;
		private var m_s3d:Sprite3D;
		private var ground:RigidBody;
		
		public function bmoregolf()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.color = 0xDDDDDD;
			addEventListener(Event.ADDED_TO_STAGE, init); 
			
		}
		private var m_building:Sprite3D = new Sprite3D();	
		private var m_ball:RigidBody;
		public function init(e:Event):void { 
			removeEventListener(Event.ADDED_TO_STAGE, init); 
			view = new View3D({width: stage.stageWidth, height: stage.stageHeight, x:stage.stageWidth/2, y:stage.stageHeight/2}); 
			addChild(view);
			physics = new Away3DPhysics(view, 6); 
			m_inputManager = new InputManager(stage, new DefaultInputHandler(this.physics));
			view.camera.pan(45);
			view.camera.tilt(31.5);
			
			view.camera.x = -950;
			view.camera.y = 100;
			
			this.ground = physics.createGround({width:1200, height: 1200}, -1000);
			//this.ground.rotationX = 2;
			view.scene.ownCanvas = true;
			view.scene.pushback = true;
			Away3dMesh(ground.skin).mesh.material = new WireframeMaterial(0); 

			addEventListener(Event.ENTER_FRAME, render); 
						
			
			for(var i:int = 0; i < 20; i++){
				var x:int = Math.round(Math.random()*20)*50 - 500;
				var z:int = Math.round(Math.random()*20)*50 - 500;
				y = 0;
				createBox(new Vector3D(x, y, z));
			}
			Away3dMesh(m_box.skin).mesh.material = new WireframeMaterial(0);		
			
			//m_inputManager.setInputHandler(new CameraInputHandler(view.camera));
			view.camera.lens = new OrthogonalLens();
			view.camera.zoom = 60;
			
			//view.camera.zoom = 12;
			
			
		}
		

		
		public function createBox(pos:Vector3D):void{
			m_box = physics.createCube({width:100, height:150, depth:100});
			m_box.material.restitution = 0;
			m_box.moveTo(pos);
			m_box.mass = 100000;
			m_box.movable = false;
			m_box.y = -1000 + 75;
			
		}
		
		private function render(event:Event):void {
			physics.step(); 
			view.render(); 
			
			
		} 		
	}
}